1196F - K-th Path - CodeForces Solution


brute force constructive algorithms shortest paths sortings *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

typedef pair<long long, long long> Point;

#define ll long long
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
#define MOD 1000000007
#define MOD1 1000000009
#define sqr(x) sqr((x)*(x))

void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef BE_DEBUG
#define debug(...) cerr << "\033[1;31m(" << #__VA_ARGS__ << "):\033[0m", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

const int N = 200005;

int n,m,k;
pair<int,pair<int,int>> p[N];
map<int,int> mp;
map<int,int> :: iterator it;
ll ans[850][850];
vector<ll> tmp;

int main() {
    IOS
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i ++) {
        cin >> p[i].ss.ff >> p[i].ss.ss >> p[i].ff;
    }
    sort(p + 1, p + m + 1);
    for(int i = 1; i <= k; i ++) {
        mp[p[i].ss.ff] ++;
        mp[p[i].ss.ss] ++;
    }
    int cnt = 0;
    for(it = mp.begin(); it != mp.end(); it ++) {
        mp[it -> ff] = cnt;
        cnt ++;
    }
    for(int i = 1; i <= k; i ++) {
        p[i].ss.ff = mp[p[i].ss.ff];
        p[i].ss.ss = mp[p[i].ss.ss];
        //
        ans[p[i].ss.ff][p[i].ss.ss] = p[i].ff; 
        ans[p[i].ss.ss][p[i].ss.ff] = p[i].ff;
        // cout << p[i].ss.ff << ' ' << p[i].ss.ss << ' ' << p[i].ff << '\n';
    }
    for(int k = 0; k < cnt; k ++) {
        for(int i = 0; i < cnt; i ++) {
            for(int j = 0; j < cnt; j ++) {
                if(ans[i][k] == 0 || ans[k][j] == 0) continue;
                if(ans[i][j] == 0) ans[i][j] = ans[i][k] + ans[k][j];
                else ans[i][j] = min(ans[i][j], ans[i][k] + ans[k][j]);
            }
        }
    }
    for(int i = 0; i < cnt; i ++) {
        for(int j = i + 1; j < cnt; j ++) {
            if(ans[i][j] != 0) tmp.pb(ans[i][j]);
        }
    }
    sort(tmp.begin(), tmp.end());
    cout << tmp[k - 1];
    return 323/1001;
}


Comments

Submit
0 Comments
More Questions

1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple